//
//  SettingController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/9/30.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SettingController.h"
#import "FirstViewController.h"
#import "CBPointController.h"
#import "SCAgreementController.h"
#import "SCAboutController.h"
#import "SCChangeTPController.h"
#import "SCSetLoginPwdController.h"

@interface SettingController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *tableView;

@property (nonatomic,strong)NSArray *data;

@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"设置";
//    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    [self initData];
}
- (void)setUI{
    
    UIButton *logOut = [UIButton buttonWithType:UIButtonTypeCustom];
    [logOut setTitle:@"退出登录" forState:UIControlStateNormal];
    [logOut setTitleColor:KOrange forState:UIControlStateNormal];
    [logOut addTarget:self action:@selector(logOut:) forControlEvents:UIControlEventTouchUpInside];
    logOut.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [logOut setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:logOut];

    
    __weak __typeof(&*self)weakSelf = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf.view);
        make.height.mas_equalTo(44*5+20);
    }];
    [logOut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tableView.mas_bottom);
        make.centerX.mas_equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(kWidth, 44));
    }];

}
- (void)logOut:(UIButton *)btn{
    
//    __weak __typeof(&*self)weakSelf = self;
    [SCTool showAlerts:@"退出登录?" Ok:^(id alerts) {
        
        [SCUserTool logOut];
//         [SCTool loginVc:self];
         [UIApplication sharedApplication].delegate.window.rootViewController =[[JTNavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
    } Cancel:^(id alerts) {
        [SCTool hideHUD];
    }];
    
    
}


- (void)initData{
    
    _data  =  @[ @[@{@"icon":@"del",@"title":@"清空缓存",@"detailTitle":@"",@"vc":@""},
                   @{@"icon":@"agree",@"title":@"商家协议",@"detailTitle":@"",@"vc":[SCAgreementController class]},
                   @{@"icon":@"huanbang",@"title":@"换绑手机",@"detailTitle":[SCUserTool loadInfo].member_mobile,@"vc":[SCChangeTPController class]},
                   @{@"icon":@"btn_settingpwd",@"title":@"登录密码",@"detailTitle":@"设置/修改",@"vc":[SCSetLoginPwdController class]}],
                 @[@{@"icon":@"about",@"title":@"关于我们",@"detailTitle":@"",@"vc":[SCAboutController class]}],
                 ];
    [_tableView reloadData];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *setData = _data[section];
    return setData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identify"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"identify"];
    }
     NSArray *setData = _data[indexPath.section];
    
    NSDictionary *dic = setData[indexPath.row];
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = dic[@"detailTitle"];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.imageView.image = [UIImage imageNamed:dic[@"icon"]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    __weak __typeof(&*self)weakSelf = self;
    if (indexPath.section == 0 && indexPath.row == 0) {
        [SCTool showAlerts:@"确定要清除缓存?" Ok:^(id alerts) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SCTool showMessage:@"清除成功"];
            });
            
        } Cancel:^(id alerts) {
            [SCTool hideHUD];
        }];
        return;
    }
    NSArray *sectionArr = _data[indexPath.section];
    NSDictionary *dic = sectionArr[indexPath.row];
    if ([dic[@"vc"] isKindOfClass:[NSString class]]) {
        return;
    }
    UIViewController *vc = [[dic[@"vc"] alloc] init] ;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableView *)tableView{
    
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.rowHeight = 44;
        tableView.separatorColor = WYColor(238, 238, 238);
        tableView.backgroundColor = WYColor(238, 238, 238);
        tableView.tableFooterView = [[UIView alloc] init];
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
